package io.ede.modules.ede.controller;

import java.util.*;

import com.google.common.collect.Maps;
import io.ede.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.ede.modules.ede.entity.ModelEntity;
import io.ede.modules.ede.service.ModelService;
import io.ede.common.utils.PageUtils;
import io.ede.common.utils.R;


/**
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-08-27 13:28:08
 */
@RestController
@RequestMapping("ede/model")
public class ModelController {
    @Autowired
    private ModelService modelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ede:model:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = modelService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ede:model:info")
    public R info(@PathVariable("id") Integer id) {
        ModelEntity model = modelService.getById(id);

        return R.ok().put("model", model);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ede:model:save")
    public R save(@RequestBody ModelEntity model) {
        model.setDelFlag("1");
        model.setCreateTime(new Date());
        modelService.save(model);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ede:model:update")
    public R update(@RequestBody ModelEntity model) {
        ValidatorUtils.validateEntity(model);
        model.setUpdateTime(new Date());
        modelService.updateById(model);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ede:model:delete")
    public R delete(@RequestBody Integer[] ids) {
        ModelEntity model = new ModelEntity();
        model.setDelFlag("0");
        model.setUpdateTime(new Date());
        for (int i = 0; i < ids.length; i++) {
            model.setId(ids[i]);
            modelService.updateById(model);
        }
        return R.ok();
    }

    /**
     * 查询所有机型下拉
     *
     * @return
     */
    @RequestMapping("/getAll")
    public R getAll() {
        Map<String, Object> params = new HashMap<>();
        params.put("del_flag", "1");
        Collection<ModelEntity> modelEntityList = modelService.listByMap(params);
        return R.ok().put("modelEntityList", modelEntityList);
    }

}
